

package com.blb.ssm.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blb.ssm.entity.User;
import com.blb.ssm.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("")
	public String show(){
		return "login";
	}
	
	@RequestMapping("/orderindex")
	public String showTab(){
		return "orderindex";
	}
	
	@RequestMapping("/login")
	public String login(String username,String password,HttpServletRequest request){
		User user = userService.login(username, password);
		if (user != null) {
			request.setAttribute("msg", user.getUname());
			return "index";
		}else {
			request.setAttribute("error", "用户名或密码错误！");
			return "login";
		}
	}
	
	
	@RequestMapping("/userindex")
	public String userindex(){
		return "user";
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public Map<String, Object> getlist(String uname,String username,int page,int limit){
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> ulist = userService.getlist(uname,username,page,limit);
		int count = userService.getcount(uname,username);
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", ulist);
		return map;
		
	}
	
	@ResponseBody
	@RequestMapping("/del")
	public String deleteuser(String uid){
		if (uid.equals("001")) {
			return "";
		} else {
		try {
			userService.deluser(uid);
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	}
	
	
	@ResponseBody
	@RequestMapping("/add")
	public Map<String,Object> adduser(Model model,User user){
		Map<String, Object> map = new HashMap<String,Object>();
		String msg=null;
		try {
			//System.out.println(user);
			user.setUid(UUID.randomUUID().toString());
			msg=userService.adduser(user);
			map.put("code", "success");
			map.put("msg", msg);
		} catch (Exception e) {
			map.put("code", "error");
			e.printStackTrace();
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public Map<String,Object> updateSupplier(User user){
		Map<String, Object> map = new HashMap<String,Object>();
		String msg=null;
		if (user.getUid().equals("001")) {
			msg = "管理员不允许更改";
			map.put("code", "success");
			map.put("msg", msg);
			return map;
		} else {
			try {
				msg=userService.updateuser(user);
				map.put("code", "success");
				map.put("msg", msg);
			} catch (Exception e) {
				map.put("code", "error");
				e.printStackTrace();
			}
			return map;
		}
		
	}
	
}
